package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.PersonalJournalBiz;
import com.njym.java.security.app.entity.PersonalJournal;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("personalJournal")
public class PersonalJournalController extends BaseController<PersonalJournalBiz,PersonalJournal> {

    //用户获取自己的日志
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam String userId){
        if (!userId.equals("undefined")) {
            List<PersonalJournal> list = baseBiz.getLists(Integer.parseInt(userId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<PersonalJournal>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    //用户获取好友的日志
    @RequestMapping(value = "/getOpenJournals",method = RequestMethod.GET)
    @ResponseBody
    public Object getOpenJournals(@RequestParam String userId){
        if (!userId.equals("undefined")) {
            PersonalJournal entity = new PersonalJournal();
            entity.setCreateUser(Integer.parseInt(userId));
            entity.setIsShow(1);
            entity.setDeleted(0);
            List<PersonalJournal> list = baseBiz.selectList(entity);
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<PersonalJournal>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

//    删除日志
    @RequestMapping(value = "/deleteJournal",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteJournal(@RequestParam String id, @RequestParam String userId){
        if (!userId.equals("undefined") && !id.equals("undefined")) {
            int deleted =  baseBiz.deleteJournal(Integer.parseInt(id), Integer.parseInt(userId));
            return new ObjectRestResponse().rel(true).data(deleted);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

//    创建
    @RequestMapping(value = "/addJournal",method = RequestMethod.GET)
    @ResponseBody
    public Object addJournal(@RequestParam String title, @RequestParam String content,
                                            @RequestParam String createUser, @RequestParam String isShow){
        if (!createUser.equals("undefined") && !isShow.equals("undefined")) {
            int adds = baseBiz.addJournal(title, content, Integer.parseInt(createUser), Integer.parseInt(isShow));
            return new ObjectRestResponse().rel(true).data(adds);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

}